package com.project.sandbox.controller;

import com.project.sandbox.core.cpp.CppCodeSandbox;
import com.project.sandbox.core.java.JavaCodeSandbox;
import com.project.sandbox.core.python.Python3CodeSandbox;
import com.project.sandbox.model.ExecuteCodeRequest;
import com.project.sandbox.model.ExecuteCodeResponse;
import com.project.sandbox.model.enums.QuestionSubmitStatusEnum;
import com.project.sandbox.model.enums.SupportLanguageEnum;
import com.project.sandbox.utils.EnumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/sandbox")
public class SandboxController {
    @Autowired
    private JavaCodeSandbox javaCodeSandbox;
    @Autowired
    private Python3CodeSandbox python3CodeSandbox;
    @Autowired
    private CppCodeSandbox cppCodeSandbox;

    @GetMapping("/health")
    public String healthCheck() {
        return "ok";
    }

    @PostMapping("/run")
    ExecuteCodeResponse runCode(@Valid @RequestBody ExecuteCodeRequest executeCodeRequest) {
        SupportLanguageEnum supportLanguage = EnumUtils.getByCode(SupportLanguageEnum.class, executeCodeRequest.getLanguage());
        if (SupportLanguageEnum.JAVA.equals(supportLanguage)) {
            return javaCodeSandbox.executeCode(executeCodeRequest);
        } else if (SupportLanguageEnum.PYTHON3.equals(supportLanguage)) {
            return python3CodeSandbox.executeCode(executeCodeRequest);
        } else if (SupportLanguageEnum.CPP.equals(supportLanguage)) {
            return cppCodeSandbox.executeCode(executeCodeRequest);
        } else {
            return new ExecuteCodeResponse(null, "不支持的编程语言", QuestionSubmitStatusEnum.FAILED.getCode(), null);
        }
    }
}

